function submit_api()
{
	var allow_sub = checkPost();
	if(allow_sub)
	{
		var datastr = allow_sub;
		var r = Math.random();
		$.ajax({
	        type: "POST",
	        url: "/admin/aj_publiclog.php?r=" + r,
	        data: "action=edit" + datastr,
	        dataType: "json",
	        success: function(json){
	            if(json.status) {
	            	showCommonLayer(json.msg, 4);
						location.href = 'public_log.php';
						return;
	            } else {
	            	showCommonLayer(json.msg, 5);
	            }
	        }
	    }); 
	}
}

//检查表单填写情况（除属性外，每一项都为必填）
function checkPost()
{
	//获取各查询条件
    var db_flag = $('select[name="db_flag"]').val();	//数据库配置名称
        db_flag = $.trim(db_flag);
    var tb_name = $('input[name="tb_name"]').val();	//要索引的数据表名称
        tb_name = $.trim(tb_name);
    var tb_id_name = $('input[name="tb_id_name"]').val();	//要索引的数据表名称
        tb_id_name = $.trim(tb_id_name);
    var timefield_name = $('input[name="timefield_name"]').val(); //数据表中的时间字段名称
        timefield_name = $.trim(timefield_name);
    var sql = $('textarea[name="SQL"]').val();	//主索引SQL语句
        sql = $.trim(sql);
    
    //检验“选择数据库”是否已选
    if(db_flag == 0){
    	showCommonLayer('请选择数据库', 1);
		return false;
    }
	
	//检验“建立索引的数据表名”是否填写
	if(!tb_name && tb_name.length==0){
		showCommonLayer('请填写建立索引的数据表名', 1);
		return false;
	}
	
	//检验“建立索引的数据表ID字段名”是否填写
	if(!tb_id_name && tb_id_name.length==0){
		showCommonLayer('请填写建立索引的数据表ID字段名', 1);
		return false;
	}
	
	//检验“表中的时间字段名”是否填写
	if(!timefield_name && timefield_name.length==0){
		showCommonLayer('请填写表中的时间字段名', 1);
		return false;
	}
	
	//属性字段（批量）
    var fieldname = '|';
    $('input[name="fieldname"]').each(function(){
    	fieldname += $(this).val() + '|';
    });
    
    //属性字段类型（批量）
    var fieldtype = '|';
    $('select[name="fieldtype"]').each(function(){
    	fieldtype += $(this).val() + '|';
    });
    
    //全文索引字段（批量）
    var fieldname_f = '|';
    $('input[name="fieldname_f"]').each(function(){
    	fieldname_f += $(this).val() + '|';
    });
    
    //全文字段类型（批量）
    var fieldtype_f = '|';
    $('select[name="fieldtype_f"]').each(function(){
    	fieldtype_f += $(this).val() + '|';
    });
    
    var is_delta = 0;
    $('input[name="is_delta"]').each(function(){
    	if($(this).attr('checked') == true){
    		is_delta = $(this).val();
    	}
    });
    
    var status = 1;
    $('input[name="status"]').each(function(){
    	if($(this).attr('checked') == true){
    		status = $(this).val();
    	}
    });

    var datastr = '';
        datastr += '&db_flag=' + db_flag;
        datastr += '&tb_name=' + tb_name;
        datastr += '&tb_id_name=' + tb_id_name;
        datastr += '&timefield_name=' + timefield_name;
        datastr += '&is_delta=' + is_delta;
        datastr += '&config_status=' + status;
        datastr += '&fieldname=' + fieldname;
        datastr += '&fieldtype=' + fieldtype;
        datastr += '&fieldname_f=' + fieldname_f;
        datastr += '&fieldtype_f=' + fieldtype_f;
        datastr += '&sql=' + sql;

    //无论是修改还是提交，都获取id输入框的值，如果值为空，就不传这个参数
    var id = $('input[name="id"]').val();
        id = $.trim(id);
    if(id.length>0 && id){
        datastr += '&id=' + id;
    } 
        
    //调用接口继续检查各字段的真实性和可用性
    $.ajax({
	    type: "POST",
	    url: "/admin/aj_publiclog.php",
	    data: "action=checkValidate" + datastr,
	    dataType: "json",
	    success: function(json){
	        if(!json.status) {
	          	showCommonLayer(json.msg, 4);
	          	return false;
	        } 
	        else {
	        	showCommonLayer('验证完毕，可以提交审核', 4);
	        }
	    }
    }); 
    return datastr;
}

function add_param(span_id, field_type)
{
	var span_id = parseInt(span_id);
	var field_type = field_type;
	//找到当前总共有多少参数的span，以便为它们分配name号
	var max_span_id = 0;
	var parent = $('span[name="'+span_id+'"][rel="'+field_type+'"]').parent().find('span').each(function(){
		max_span_id = $(this).attr('name');
	});
	var new_span_id = parseInt(max_span_id) + 1;
	
	var html = '<span name="'+new_span_id+'" style="border-right:0px" rel="'+field_type+'"><br>';
	if(field_type == 'fulltext'){
		html += '<input type="text" name="fieldname_f" class="Adm_input Adm_input01" style="width:100px;vertical-align:middle;" />&nbsp;';
        html += '<select name="fieldtype_f" class="Adm_select01" style="width:50px;vertical-align:middle;"><option value="int">int</option><option value="string">string</option></select>&nbsp;';
	} else {
		html += '<input type="text" name="fieldname" class="Adm_input Adm_input01" style="width:100px;vertical-align:middle;" />&nbsp;';
        html += '<select name="fieldtype" class="Adm_select01" style="width:50px;vertical-align:middle;"><option value="int">int</option><option value="string">string</option></select>&nbsp;';
	}
        html += '<button type="button" onclick="add_param('+new_span_id+',\''+field_type+'\');" style="width:20;">+</button>&nbsp;';
        html += '<button type="button" onclick="del_param('+new_span_id+',\''+field_type+'\');" style="width:20;">-</button>';
        html += '</span>';
     $('span[name="'+span_id+'"][rel="'+field_type+'"]').parent().append(html);
}

function del_param(span_id, field_type)
{
	$('span[name="'+span_id+'"][rel="'+field_type+'"]').remove();
}

//根据db_config下拉列表获取数据库信息
function getDBConfig(){
	var db_flag = $('select[name="db_flag"]').val();
	if(db_flag == 0) {
		$('#checkserver').hide();
		return false;
	}
	var datastr = '&db_flag=' + db_flag;
	$.ajax({
        type: "POST",
        url: "/admin/aj_publiclog.php",
        data: "action=getDBConf"+datastr,
        dataType: "json",
        success: function(json){
        	var html = '';
        	var options = '';
            if(json.status) {
            	$('#checkserver').show();
            	html += '<br>Host：' + json.dbconf['host'] + '，';
            	html += 'Port：' + json.dbconf['port'] + '，';
            	html += 'DB：' + json.dbconf['dbname'];
            } else {
            	html += '<br>'+json.dbconf['msg'];
            }
            $('#checkserver').html(html);
        }
    }); 
}

